package com.fita.admin.entity.search.data;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import org.apache.commons.lang3.BooleanUtils;

import java.util.Map;

/**
 * @author 11200
 *
 */
public class CountrySearch implements QueryBuilder {

    private String code;

    private String englishName;

    private String chineseName;
    
    private Boolean isAlamo;
    
    private Boolean isNational;
    
    private Boolean isEnterprise;
	
	private QueryFilter filter;

	private QueryOrder order;

	
	
	public CountrySearch() {
		
		order = new QueryOrder();
	}

	@Override
	public QueryFilter getPreparedFilter(boolean refresh) {
		
		if (filter == null || refresh) {
        	
            filter = new QueryFilter().addLike("code", code)
            						  .addLike("englishName", englishName)
            						  .addLike("chineseName", chineseName);
            
            if (BooleanUtils.isTrue(isAlamo)) {
            	
            	filter.addLike("suppliers", "Alamo");
			}
            if (BooleanUtils.isFalse(isAlamo)) {
            	
				filter.addSentence("suppliers NOT LIKE :suppliers", "suppliers", "%Alamo%");
			}
            if (BooleanUtils.isTrue(isNational)) {
            	
            	filter.addLike("suppliers", "National");
			}
            if (BooleanUtils.isFalse(isNational)) {
            	
				filter.addSentence("suppliers NOT LIKE :suppliers", "suppliers", "%National%");
			}
            if (BooleanUtils.isTrue(isEnterprise)) {
            	
            	filter.addLike("suppliers", "Enterprise");
			}
            if (BooleanUtils.isFalse(isEnterprise)) {
				
				filter.addSentence("suppliers NOT LIKE :suppliers", "suppliers", "%Enterprise%");
			}
        }
        return filter;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
	 */
	public QueryOrder getOrder() {
		
		return order;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
	 */
	public String buildSentenceTemplate() {
		
		String sentence = "FROM Country Where disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
		
        return sentence;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
	 */
	public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getEnglishName() {
		return englishName;
	}

	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}

	public String getChineseName() {
		return chineseName;
	}

	public void setChineseName(String chineseName) {
		this.chineseName = chineseName;
	}

	public Boolean getIsAlamo() {
		return isAlamo;
	}

	public void setIsAlamo(Boolean isAlamo) {
		this.isAlamo = isAlamo;
	}

	public Boolean getIsNational() {
		return isNational;
	}

	public void setIsNational(Boolean isNational) {
		this.isNational = isNational;
	}

	public Boolean getIsEnterprise() {
		return isEnterprise;
	}

	public void setIsEnterprise(Boolean isEnterprise) {
		this.isEnterprise = isEnterprise;
	}
	
}
